package me.hl.javaslang;

import javaslang.control.Either;

import java.util.Optional;

/**
 * Created by haoliang on 2016/7/13.
 */
public class EitherTest {

    public static Either<Integer, String> divide(int a, int b) {

        Optional<Integer> opb = Optional.ofNullable(b).filter((x) -> x != 0);
        if (opb.isPresent()) {
            return Either.left(a / b);
        } else {
            return Either.right("can not divide zero");
        }
    }


    public static void main(String args[]) {
        Either either = divide(1, 1);
        if (either.isLeft()) {
            System.out.println(either.getLeft());
        } else {
            System.out.println(either.get());
        }
    }

}
